<template>
	<coreshop-page :title="state.title" mode="left">
		<view class="layout-notice-box">
			<view v-if="state.list.length > 0">
				<view class="item-box" v-for="item in state.list" :key="item.id"
					@click="handleRouteNavigateTo(`/pages/subpackage/article/detail/detail?id=${item.id}`)">
					<view class="title">{{ item.title }}</view>
					<uv-icon name="arrow-right" size="18"></uv-icon>
				</view>
				<view class="no-data" v-if="state.page === state.totalPages">没有更多了</view>
			</view>
			<view v-else class="layout-empty-box">
				<coreshop-empty :mode="EmptyEnum.data" text="暂无文章列表"></coreshop-empty>
			</view>
		</view>

	</coreshop-page>
</template>
<script setup lang="ts">
	import { reactive } from 'vue';
	import { onLoad, onReachBottom } from '@dcloudio/uni-app';
	import { queryArticleList } from '@/core/api';
	import type { Response } from '@/core/models';
	import { EmptyEnum } from '@/core/enum';
	import { handleRouteNavigateTo } from '@/core/utils';

	interface QueryParams {
		id : number;
	}

	const state = reactive<{
		list : Array<any>;
		page : number;
		limit : number;
		id : number;
		totalPages : number;
		title : string;
	}>({
		list: [],
		page: 1,
		limit: 10,
		id: 0,
		totalPages: 1,
		title: "文章列表"
	})

	onLoad((query : QueryParams) => {
		state.id = Number(query.id);
		getArticleList();
	})

	/** 获取文章列表 */
	const getArticleList = async () => {
		uni.showLoading({
			title: "加载中..."
		})
		const articleList : Response<any> = await queryArticleList({
			id: state.id,
			page: state.page,
			limit: 20,
		});
		state.title = articleList.data?.articleType?.name;
		state.totalPages = articleList.data?.totalPages;
		state.list = state.list.concat(articleList.data?.list);
		uni.hideLoading();
	}

	onReachBottom(() => {
		if (state.totalPages > state.page) {
			state.page++;
			getArticleList();
		}
	});
</script>
<style lang="scss" scoped>
	@import './category.scss';
</style>